package com.yuntian.service.Impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuntian.Dto.ProductPageQueryDto;
import com.yuntian.entity.Product;
import com.yuntian.entity.User;
import com.yuntian.exception.BaseException;
import com.yuntian.mapper.ProductMapper;
import com.yuntian.result.PageResult;
import com.yuntian.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {

    //查询商品列表
    @Autowired
    private ProductMapper productMapper;
    public PageResult list(ProductPageQueryDto productPageQueryDto) {
        PageHelper.startPage(productPageQueryDto.getPage(),productPageQueryDto.getPageSize());
        Page<Product> page = productMapper.selectList(productPageQueryDto);
        long total = page.getTotal();
        List<Product> result = page.getResult();
        return new PageResult(total,result);
    }

    //商品新增
    public void insertProduct(Product product) {
        product.setCreateTime(LocalDateTime.now());
        product.setUpdateTime(LocalDateTime.now());
        Product product1 = productMapper.selectByName(product.getProductName());
        if(product1 != null){
            throw new BaseException("商品已存在,请勿重复添加");
        }
        productMapper.insertProduct(product);
    }

    //商品修改
    public void updateProduct(Product product) {
        product.setUpdateTime(LocalDateTime.now());
        productMapper.updateProduct(product);
    }

    //商品删除
    public void deleteProduct(Product product) {
        product.setProductStatus(0);
        product.setUpdateTime(LocalDateTime.now());
    }



}
